package com.yh.csx.bsf.ods.controller;

import java.text.SimpleDateFormat;
import java.util.Map.Entry;
import java.util.Optional;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yh.csx.bsf.core.util.PropertyUtils;
import com.yh.csx.bsf.ods.monitor.CheckSchedule;
import com.yh.csx.bsf.ods.monitor.CheckSchedule.CheckTask;
@Controller
@RequestMapping("/")
public class TaskController {
	final static String DATETIME_FORMAT="yyyy-MM-dd HH:mm:ss";

	@GetMapping("/")
	@ResponseBody
	public String info() {		
		return this.checkstatus();
	}

	private String checkstatus() {
		StringBuilder sb=new StringBuilder();
		// 刷新进度
		for (Entry<String,CheckTask> t :CheckSchedule.CHECK_TABLE_TASKS.entrySet()) {
			sb.append(t.getValue().getTargetTable()).append("总条数差异[").append(Optional.ofNullable(t.getValue().getSumDiffCount()).orElse(0L)).append("]");
			boolean warn=false;
			if(Math.abs(Optional.ofNullable(t.getValue().getRelativeDiffCount()).orElse(0L)) > PropertyUtils.getPropertyCache("bsf.health.strategy.sync.tables.diffcount", 100)) {
				warn=true;
			}
			if(warn) {
				sb.append("<font color=\"red\">");
			}
			sb.append("相对条数差异[").append(Optional.ofNullable(t.getValue().getRelativeDiffCount()).orElse(0L)).append("]");
			sb.append("内容差异[").append(Optional.ofNullable(t.getValue().getContentDiffCount()).orElse(0L)).append("]");
			if(warn) {
				sb.append("</font>");
			}
			if(t.getValue().getChecktime()!=null) {
				sb.append("检查时间").append(new SimpleDateFormat(DATETIME_FORMAT).format(t.getValue().getChecktime()));
			}
			sb.append("\r\n");
			
		}
		return sb.toString().replace("\r\n", "<br/>");
	}
	
}
